var EventDispatcher = (function() {
    
    function _EventDispatcher(){}  
    
    _EventDispatcher.parseEvents = function(events, everyEventCallback, parseEventDoneCallback) { 
        events = events || [];        
        events.forEach(function(evt) {            
            try {
                //添加会话
                if (evt['eventType'] === EventType.ADDTOPICENTITY) {
                    if(evt['te']['type'] != 'robotpost.wallboard'){                 
                        page.chatGroupStore.addItem(evt);
                    } else {                    
                        $('span[tbi-id="wallboard"]').show().data('te', evt['te']);
                    }
                }
                //更新会话
                else if (evt['eventType'] === EventType.UPDATETOPICENTITY) {                    
                    var wallboardEntity = $('span[tbi-id="wallboard"]').data('te');
                    if(!wallboardEntity || wallboardEntity['id'] != evt['teid']){
                        page.chatGroupStore.updateItem(evt);                        
                    }
                } 
                //删除会话
                else if (evt['eventType'] === EventType.DELETETOPICENTITY) {
                    chatGroupList.cardList.deleteItem(evt['teid']);
                    sessionList.deleteSession(evt['teid']);
                    
                    var wallboardEntity = $('span[tbi-id="wallboard"]').data('te');
                    if(wallboardEntity && wallboardEntity['id'] == evt['teid']){
                        $('span[tbi-id="wallboard"]').hide();
                    }
                    
                    try {
                        if(page.chatGroupStore.getItem(evt['teid']).type != 'singlechat'){
                            page.chatGroupStore.deleteItem(evt['teid']);
                        }
                    } catch (e){}
                }
                
                //添加Link
                else if (evt['eventType'] === EventType.ADDURLENTITY) { 
                    page.urlStore.addItem(evt);
                } 
                //更新Link
                else if (evt['eventType'] === EventType.UPDATEURLENTITY) {                
                    page.urlStore.updateItem(evt);
                } 
                //删除Link
                else if (evt['eventType'] === EventType.DELETEURLENTITY) {
                    page.urlStore.deleteItem(evt['ueid']);   
                    sessionList.deleteSession(evt['ueid']);             
                }
                
                //更新User Profile
                else if (evt['eventType'] === EventType.UPDATEUSER) {                
                    ujs.util.session.setCurrentUserInfo(evt['user']);   
                }                
                //添加消息.
                else if (evt['eventType'] === EventType.ADDTOPIC) {
                    page.chatMsgStore.addItem(evt['t']);
                } 
                //消息已读标识.
                else if (evt['eventType'] === EventType.TOPICREAD) {
                    var teid = evt['teid'], 
                        latestSeq = evt['seq'],
                        activePanel = ChatPanel.getActiveChatPanel(),
                        te = page.chatGroupStore.getItem(teid);
                                            
                    if (activePanel && activePanel.chatGroupEntity['id'] == teid) {
                        if (te.type === 'singlechat') {
                            activePanel.chatView.setMsgReaded(latestSeq);
                        }
                        page.biz.getReadCount.defer(200, page.biz);
                    } else {
                        if(evt['uid'] == ujs.util.session.getCurrentUserID()){                    
                            sessionList.updateItem(teid, function(teObj){                            
                                if(latestSeq > teObj['currentSeq']){
                                    teObj['currentSeq'] = teObj['latestSeq'] = latestSeq;
                                }
                            });
                            //(function(){
                                //page.setUnread(true);
                            // }).defer();
                            
                            window.setTimeout(function(){
                                page.setUnread(undefined, false);
                            }, 200);
                        } 
                    }
                }
                else if(evt['eventType'] === EventType.REPORTLOCATION){ //请求位置.
                    page.biz.reportLocationIf(evt);
                }
                else if(evt['eventType'] === EventType.ADDMAPUSER) {    //location user add marker.                                
                    // if(ChatPanel.isActiveLocationAlert(evt['teid'])){
                        // ChatPanel.getActiveChatPanel().addUser(evt);
                    // }              
                    var cp = page.contentCardLayout.getItem(evt['teid']);
                    if(cp){
                        cp.addUser(evt);
                    }
                    
                } else if(evt['eventType'] === 61 && evt['commandType'] == 'logs'){
                    var lid = evt['lid'];
                    if(lid){
                        $.ajax({
                            type : 'post',
                            url : '/apis/user/logs/' + lid,
                            data : JSON.stringify(ujs.util.debug.getLogs()),
                            processData : false,
                            success : function(msg){}
                        });
                    }
                }               
            } catch(e) {
                console.log(e);
                ujs.util.debug.error(e);
            }          
                    
            everyEventCallback && everyEventCallback(evt);
        });        
        parseEventDoneCallback && parseEventDoneCallback(events);
    };

    return _EventDispatcher;
})();